/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.colswe.solestac.entity.Instance;

/**
 *
 * @author David Cendales
 */
public class InstanceDao {
    
    /**Search an instance by the name.
     * @param n Instance name given by the user
     * @param em EntityManager object to make the operation
     * @return The instance with the information related to the name given
     */
    public List<Instance> searchByName(final String n, final EntityManager em) {
        Query q = em.createNamedQuery("Instance.findByName").setParameter("name", n);
        List<Instance> instance = new ArrayList<Instance>();
        instance.add((Instance) q.getSingleResult());
        return instance;
    }
    
    /**Search all instances in the system.
     * @param em EntityManager object to make the operation
     * @return All registered instances
     */
    public List<Instance> searchAll(final EntityManager em) {
        Query q = em.createNamedQuery("Instance.findAll");
        return (List<Instance>) q.getResultList();
    }
    
     /** Update an instance.
     * @param i The instance object that will be updated
     * @param em EntityManager object to make the operation
     */
    public void update(final Instance i, final EntityManager em) {
        em.merge(i);
    }
    
}
